<script setup lang="ts">
import { ref, computed } from "vue";
import ReCol from "@/components/ReCol";
import { formRules } from "./utils/rule";
import type { FormItemProps, AgentOption } from "./utils/types";
import { ElForm, ElFormItem, ElInput, ElSelect, ElOption } from "element-plus";

interface Props {
  formInline: FormItemProps & {
    agentOptions?: AgentOption[];
  };
}

const props = withDefaults(defineProps<Props>(), {
  formInline: () => ({
    account_no: "",
    ad_agent_id: undefined,
    remark: "",
    agentOptions: []
  })
});

const ruleFormRef = ref();
const newFormInline = ref(props.formInline);

// 获取当前选中代理商的平台信息
const selectedAgent = computed(() => {
  return props.formInline.agentOptions?.find(
    agent => agent.value === newFormInline.value.ad_agent_id
  );
});

function getRef() {
  return ruleFormRef.value;
}

function getFormData() {
  return newFormInline.value;
}

defineExpose({ getRef, getFormData, newFormInline });
</script>

<template>
  <el-form
    ref="ruleFormRef"
    :model="newFormInline"
    :rules="formRules"
    label-width="100px"
    class="mx-auto max-w-2xl"
  >
    <el-row :gutter="30">
      <re-col :value="24">
        <el-form-item label="广告账号" prop="account_no">
          <el-input
            v-model="newFormInline.account_no"
            clearable
            placeholder="请输入广告账号"
            maxlength="255"
            show-word-limit
          />
        </el-form-item>
      </re-col>

      <re-col :value="24">
        <el-form-item label="代理商" prop="ad_agent_id">
          <el-select
            v-model="newFormInline.ad_agent_id"
            placeholder="请选择代理商"
            clearable
            class="w-full"
            filterable
          >
            <el-option
              v-for="agent in formInline.agentOptions"
              :key="agent.value"
              :label="`${agent.label} `"
              :value="agent.value"
            >
              <div class="flex items-center justify-between">
                <span>{{ agent.label }}</span>
                <el-tag
                  :type="agent.platform === 1 ? 'primary' : 'warning'"
                  size="small"
                  effect="plain"
                >
                  {{ agent.platform_text }}
                </el-tag>
              </div>
            </el-option>
          </el-select>
        </el-form-item>
      </re-col>

      <re-col v-if="selectedAgent" :value="24">
        <el-form-item label="所属平台">
          <el-tag
            :type="selectedAgent.platform === 1 ? 'primary' : 'warning'"
            effect="plain"
          >
            {{ selectedAgent.platform_text }}
          </el-tag>
        </el-form-item>
      </re-col>

      <re-col :value="24">
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="newFormInline.remark"
            type="textarea"
            :rows="4"
            placeholder="请输入备注信息"
            maxlength="1000"
            show-word-limit
          />
        </el-form-item>
      </re-col>
    </el-row>
  </el-form>
</template>

<style lang="scss" scoped>
:deep(.el-select-dropdown__item) {
  height: auto;
  padding: 8px 12px;
  line-height: 1.4;
}

.el-tag {
  margin-left: 8px;
}
</style>
